import axios from 'axios'
import { Message } from 'element-ui'
// 设置默认前缀
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API,
    timeout: 10000 // request timeout
})
// 请求拦截
service.interceptors.request.use( config => {
        return config
    },error => {
    return Promise.reject(error)
    }
)
// 相应拦截
service.interceptors.response.use(response => {
    const res = response.data
    const code = res.code
    if (code === 0) {
        return res
    } else {
        return res.message || 'Error'
    }
},error => {
    const res = error.response
    const { status } = res
    let msg = ''
    switch (status) {
        case 0:
        msg = '验证失败！'
        break
        case 403:
        msg = '服务器繁忙，请刷新重试！'
        break
        default:
        msg = '服务器繁忙，请刷新重试！'
        break
    }
    Message({
        message: msg,
        type: 'error',
        duration: 5 * 1000
    })
    return Promise.reject(error)
}
)
export default service